<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$type		= $mainframe->getUserStateFromRequest( "view{}$option{$act}type",		'type', 		0);

switch($task){

	case 'quickadd':
		
		$row = LM::getLMTable("Eventtype");
		$row->eventtype_name		= mosGetParam($_POST, "eventtype_name", "");
		$row->playermap_association = mosGetParam($_POST, "playermap_association", "");
			
		if(!$row->check()){
			LM::redirect("eventtypes", "Checking Eventtype Failed!");
			exit();
		}
		
		if(!$row->store()){
			LM::redirect("eventtypes", "Could not store Eventtype!");
		} else {
			$database->setQuery("ALTER TABLE #__leagueman_playermap 
										 ADD `{$row->playermap_association}` SMALLINT UNSIGNED NOT NULL ;");
			if($database->query()){
				
				$database->setQuery("DESCRIBE #__leagueman_playermap");
				$fields = $database->loadObjectList();
				
				$f  = "<?php\n\n";
				$f .= "class LM_Table_Playermap2 extends LM_Table_Playermap{\n\n";

				foreach($fields AS $field){
					$v = $field->Field;
	
					if(substr($v, 0, 4) == "cnt_") {
						$f .= "    public \$$v;\n";
					}
				}
				
				$f .= "\n";
				$f .= "    public function __construct(\$db){\n";
				$f .= "        parent::__construct(\$db);";
				$f .= "    }\n";
				$f .= "}\n";
				$f .= "?>";
				
				$ds = DIRECTORY_SEPARATOR;
				$filename = $mainframe->getCfg("absolute_path") . $ds . "components" . $ds . "com_leagueman" . $ds . "lib" . $ds . "LEAGUEMAN" . $ds . "LM" . $ds . "Table" . $ds . "Playermap2.php";
				file_put_contents($filename, $f);
				
			}
		}
		
		LM::redirect("eventtypes");
		
	break;
	
	case 'new':
	case 'edit':
	
		$row = LM::getLMTable("Eventtype");
		
		if($task == "edit"){
			$row->load( $cid[0] );
		}
		
		$template = LM::getTemplate();
		
		$template->assign("view",		"admin/form_eventtypes.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("my",			$my);
		$template->assign("row",		$row);
		
		$template->display("admin/view.tpl.php");
	break;
	
	case 'save':
		$row = LM::getLMTable("Eventtype");
		$row->save($_POST);
		
		$image = saveImage($mainframe->getCfg("absolute_path") . "/images/stories/leagueman/eventtypes/", "upload", $row->getAssociation());
		
		if($image){
			$row->eventtype_icon = $image;
		}
		
		if($row->store()){
			$msg = "OK";
		} else {
			$msg = "Could not save Eventtype";
		}
		
		LM::redirect("eventtypes", $msg);
	break;
	
	case 'cancel':
		LM::redirect("eventtypes");
	break;
	
	case "savetypes":
		
		$total = count( $cid );

		for( $i = 0; $i < $total; $i++ ) {
			
			$eventtype_id			= $database->Quote(intval($cid[$i]));
			$ordering 				= $database->Quote(intval($_POST["ordering"][$i]));
			
			$query = "UPDATE #__leagueman_eventtype 
						 SET ordering = $ordering
				   	   WHERE eventtype_id = $eventtype_id";
					   
			$database->setQuery( $query );
			
			if (!$database->query()) {
				LM::redirect("eventtypes", $database->getErrorMsg());
			}
		}
		
		LM::redirect("eventtypes");
	
	break;
	
	default:
		
		$limit		= LM::getSessionValueFromRequest( "_{$act}_limit_",	'limit',		10 );
        $limitstart = LM::getSessionValueFromRequest( "_{$act}_limitstart_", 'limitstart',	0 );
		
		$database->setQuery( "SELECT COUNT(eventtype_id) FROM #__leagueman_eventtype");
        $total		= $database->loadResult();
		$pageNav	= LM::getPageNav( $total, $limitstart, $limit, 1 );
		
		$database->setQuery( "SELECT *
                                FROM #__leagueman_eventtype
                            ORDER BY ordering
                               LIMIT $pageNav->limitstart, $pageNav->limit");
		$rows = $database->loadObjectList();
		
		$template = LM::getTemplate();
		
		$template->assign("view",		"admin/list_eventtypes.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("rows",		$rows);
		$template->assign("pageNav",	$pageNav);
		$template->assign("my",			$my);
		$template->assign("type",		$type);
		
		$template->display("admin/view.tpl.php");
	break;
}
	
?>